package com.spzx.channel.service.impl;

import com.alibaba.fastjson.JSON;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.IItemService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Service
public class IItemServiceImpl implements IItemService {

	// 注入：定义远程调用各个接口
	@Autowired
	private RemoteProductService remoteProductService;


	// TODO 技术点: 异步编排
	@Override
	public ItemVo item(Long skuId) {
		ItemVo itemVo = new ItemVo();
		// 1. 根据skuId查询商品sku信息
		CompletableFuture<ProductSku> productSkuCompletableFuture =
				CompletableFuture.supplyAsync(
						() -> {
							ProductSku productSku =
									remoteProductService.getProductSku(skuId, SecurityConstants.INNER).getData();
							if (productSku == null)
								throw new RuntimeException("商品不存在");
							itemVo.setProductSku(productSku);
							return productSku;
						}
				);

		// 2. 根据skuId查询商品信息
		CompletableFuture<Void> productCompletableFuture = productSkuCompletableFuture.thenAcceptAsync(
				(productSku) -> {
					Product product = remoteProductService.getProduct(productSku.getProductId(), SecurityConstants.INNER).getData();
					itemVo.setProduct(product);
					itemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
					itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));

				}
		);

		// 3. 获取Sku最新价格
		CompletableFuture<Void> skuPriceCompletableFuture =
				CompletableFuture.runAsync(
						() -> {
							SkuPrice skuPrice = remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER).getData();
							itemVo.setSkuPrice(skuPrice);
						}
				);


		// 4. 获取商品详情
		CompletableFuture<Void> productDetailsComCompletableFuture = productSkuCompletableFuture.thenAcceptAsync(
				(productSku) -> {
					R<ProductDetails> productDetailsResult = remoteProductService.getProductDetails(productSku.getProductId(), SecurityConstants.INNER);
					ProductDetails productDetails = productDetailsResult.getData();
					itemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
				}
		);

		// 5. 获取商品规格对应商品skuId信息
		CompletableFuture<Void> skuSpecValueComCompletableFuture =
				productSkuCompletableFuture.thenAcceptAsync(
						(productSku) -> {
							R<Map<String, Long>> skuSpecValueResult = remoteProductService.getSkuSpecValue(productSku.getProductId(), SecurityConstants.INNER);
							Map<String, Long> skuSpecValueMap = skuSpecValueResult.getData();
							itemVo.setSkuSpecValueMap(skuSpecValueMap);
						}
				);
		// 6. 获取商品库存信息
		CompletableFuture<Void> skuStockVoComCompletableFuture =
				CompletableFuture.runAsync(
						() -> {
							R<SkuStockVo> skuStockResult = remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
							SkuStockVo skuStockVo = skuStockResult.getData();
							itemVo.setSkuStockVo(skuStockVo);
						}
				);

		// 等待所有线程操作都完成，考虑顺序
		CompletableFuture.allOf(
				productSkuCompletableFuture,
				productCompletableFuture,
				skuPriceCompletableFuture,
				productDetailsComCompletableFuture,
				skuSpecValueComCompletableFuture,
				skuStockVoComCompletableFuture
		).join();
		return itemVo;
	}
}
